package com.ruoyi.web.controller.modules;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.modules.domain.Unit;
import com.ruoyi.modules.domain.YearJson;
import com.ruoyi.modules.service.YearJsonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 开放年度
 */
@RestController
@RequestMapping("/year/data")
public class YearJsonController extends BaseController {

    @Autowired
    private YearJsonService yearJsonService;

    /**
     * 查询开放年度信息
     * @param yearJson 年度
     * @return
     */
    @ApiOperation("查询开放年度信息")
    @GetMapping("/list")
    public TableDataInfo list(YearJson yearJson)
    {
        startPage();
        List<YearJson> list = yearJsonService.selectYearJsonList(yearJson);
        return getDataTable(list);
    }

    /**
     * 根据当前用户名获取已经贷款的年度，返回没有贷款的年度选项数组
     * @param userName 用户名
     * @return
     */
    @GetMapping("/{userName}")
    public AjaxResult selectYearNotInAlreadyCreditYear(@PathVariable String userName) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", yearJsonService.selectYearNotInAlreadyCreditYear(userName));
        return ajax;
    }

    /**
     * 查询开放的贷款年度数据详细
     * @param yearId 年度编号
     * @return
     */
    @GetMapping(value = "/yearById/{yearId}")
    public AjaxResult selectYearJsonById(@PathVariable Long yearId)
    {
        return AjaxResult.success(yearJsonService.selectYearJsonById(yearId));
    }

    /**
     * 新增开放的贷款年度信息
     * @param yearJson 开放年度对象
     * @return
     */
    @Log(title = "开放的贷款年度数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YearJson yearJson)
    {
        return toAjax(yearJsonService.insertYearJson(yearJson));
    }

    /**
     * 修改开放的贷款年度信息
     * @param yearJson 开放年度对象
     * @return
     */
    @Log(title = "开放的贷款年度数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody YearJson yearJson)
    {
        return toAjax(yearJsonService.updateYearJson(yearJson));
    }

    /**
     * 删除开放的贷款年度信息
     * @param yearIds 开放年度编号, 可批量
     * @return
     */
    @Log(title = "删除开放的贷款年度信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{yearIds}")
    public AjaxResult remove(@PathVariable Long[] yearIds) {
        return toAjax(yearJsonService.deleteYearJsonByIds(yearIds));
    }
}
